<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Qingwuit\Models\Address;
use App\Qingwuit\Models\Order;
use App\Qingwuit\Models\OrderGoods;
use App\Qingwuit\Models\Parlours;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class RiderController extends Controller
{
    protected $modelName = 'Rider';
    public function orderlist()
    {
        $order_status = \request()->order_status;
        $user_id = $this->getUserId('users');
//        $user_id = 121;
        $data =Order::where('riders',$user_id)
            ->select('id','user_id','store_id','deliverytime','receive_address','total_price','receive_tel','created_at')
            ->where('order_status',$order_status)
            ->get();
//        print_r($data);die;
        foreach ($data as $item =>$value){
            $address = Address::where('user_id',$value->user_id)->first();
            $par = Parlours::where('userid',$value->user_id)->first();
//            print_r($par);die;
            $value->receive_address = $address->area_info.$address->address;
            $value->receive_name = $address->receive_name;
            $store_name = DB::table('stores')->where('id',$value->store_id)->value('store_name');

            $area_info = DB::table('stores')->where('id',$value->store_id)->value('area_info');

            $store_address = DB::table('stores')->where('id',$value->store_id)->value('store_address');
            $value->longitude = $par->longitude;
            $value->latitude = $par->latitude;
//            print_r($area_info);die;
            $value->store_address =  $area_info.$store_address.$store_name;
            $value->delivery_time = $value->deliverytime;
            unset($value->deliverytime);
        }
        return $this->format($data);
    }


    public function orderdetails()
    {
        $order_id = \request()->order_id;

        $data  = Order::where('id',$order_id)
            ->select('id','user_id','store_id','deliverytime','receive_address','total_price','receive_tel')
            ->first();
        $store_name = DB::table('stores')->where('id',$data->store_id)->value('store_name');

        $area_info = DB::table('stores')->where('id',$data->store_id)->value('area_info');

        $store_address = DB::table('stores')->where('id',$data->store_id)->value('store_address');
//            print_r($area_info);die;
        $address = Address::where('user_id',$data->user_id)->first();
        $par = Parlours::where('userid',$data->user_id)->first();
        $data->receive_address = $address->area_info.$address->address;
        $data->receive_name = $address->receive_name;
        $data->store_name =  $store_name;
        $data->store_address = $area_info.$store_address;
        $data->delivery_time = $data->deliverytime;
        $data->longitude = $par->longitude;
        $data->latitude = $par->latitude;
        unset($data->deliverytime);
        $order_goods = OrderGoods::where('order_id',$data->id)->select('id','goods_name','buy_num')->get();
//        foreach ($order_goods as $k =>$v){
//            $v->buy_num = $v->buy_num * 500 /1000;
//        }
        $data->order_goods = $order_goods;
//        print_r($data);
        return  $this->success($data);
    }



    public function transferOrder()
    {

    }
}
